import config from '@/admin.config.js'
import messageAlert from './disposeTokenExpired.js';

import {
	ElMessage
} from 'element-plus'

let func = {}

func = new Proxy(func, {
	get: function(target, key) {
		return new FuncContext(key)
	}
})

function FuncContext(name) {
	this.funcname = name;
	this.response = {
		code: 200,
		datas: {},
		msg: ''
	}
}

FuncContext.prototype = {
	funcname: '', // 云函数名称
	response: {} // 统一响应格式
}

/**
 * 调用云函数
 * action controller 函数路径
 * data controller 数据参数
 * */
FuncContext.prototype.call = function(action, data, loading = true, notify = true) {
	if (loading) uni.showLoading({
		title: '请求中'
	})

	return uniCloud.callFunction({
		name: this.funcname,
		data: {
			action,
			data
		}
	}).then(res => {
		if (config.debug) {
			console.log(this.funcname + '/' + action + ' call', res)
		}
		if (res.result) {
			this.response.code = res.result.code === 0 ? 200 : res.result.code
			if (this.response.code != 200) {
				this.response.msg = res.result.msg || res.result.message
				
				// token失效
				if (this.response.code == 'TOKEN_INVALID') {
					messageAlert();
					return Promise.resolve(this.response)
				}

				if (this.response.code == 'FORBIDDEN') {
					ElMessage.error('此账号未授权')
					this.response.datas = res.result.datas || res.result
					return Promise.resolve(this.response)
				}

				if (notify) {
					ElMessage.error(this.response.msg)
				}
			} else {
				this.response.msg = res.result.msg
			}
			this.response.datas = res.result.datas || res.result
			this.response.result = res.result
			return Promise.resolve(this.response)
		}

		return Promise.reject(res)
	}).catch(err => {
		if (config.debug) {
			console.log(this.funcname + ' call catch', err)
			ElMessage(JSON.stringify(err))
		}
		return Promise.reject(err)
	}).finally(res => {
		if (loading) uni.hideLoading()
	})
}

export default func
